package com.haipumi.dao.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;

import com.haipumi.beans.haipumi_activity;
import com.haipumi.beans.haipumi_skin;
import com.haipumi.beans.haipumi_user;
import com.haipumi.dao.DBHelper;
import com.haipumi.dao.IBackCustDao;
import com.haipumi.utils.Encrypt;

public class BackCustDaoImpl implements IBackCustDao {
	DBHelper db=new DBHelper();
	
	
	
	@Override
	public List<haipumi_user> findAllUser() {
		List<Object>params=new ArrayList<Object>();
		String sql=null;
		sql="select * from haipumi_user";
		List<haipumi_user>userlist=null;
		try {
			userlist=db.finds(sql, null, haipumi_user.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userlist;
	}
	
	/**
	 * 不带参的findCount方法，用于首次查询
	 */
	@Override
	public int findCount() {
		double r = 0;
		String sql=null;
		sql="select count(*) from haipumi_user";
			
		try {
			r=db.findDouble(sql, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (int) r;
	}

	@Override
	public List<haipumi_user> seacherUser(HttpServletRequest request,
			String seacherType) {
		List<haipumi_user> userlist=null;
		List<Object>params=new ArrayList<Object>();
		String sql=null;
		String searchoptions=request.getParameter("searchOptions");;
		if(seacherType.equals("mohu")){
			//模糊查找
			sql="select * from haipumi_user where ualais like ?";
			params.add("%"+searchoptions+"%");
			try {
				userlist=db.finds(sql, params, haipumi_user.class);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(seacherType.equals("jingque")){
			//精确
			sql="select * from haipumi_user where ulogon=? ";
			params.add(searchoptions);
			try {
				userlist=db.finds(sql, params, haipumi_user.class);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(seacherType.equals("qq")){
			//精确
			sql="select * from haipumi_user where uqq=? ";
			params.add(searchoptions);
			try {
				userlist=db.finds(sql, params, haipumi_user.class);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(seacherType.equals("uid")){
			//精确
			sql="select * from haipumi_user where uid=? ";
			params.add(searchoptions);
			try {
				userlist=db.finds(sql, params, haipumi_user.class);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return userlist;
	}

	@Override
	public int findCount(HttpServletRequest request, String seacherType) {
		
		int r=0;
		String searchoptions=request.getParameter("searchOptions");
		List<Object>params=new ArrayList<Object>();
		String sql=null;
		if(seacherType.equals("mohu")){
			sql="select count(*) from haipumi_user where ualais=? ";
			params.add(searchoptions);
			try {
				r=(int) db.findDouble(sql, params);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(seacherType.equals("jingque")){
			sql="select count(*) from haipumi_user where ulogon=? ";
			params.add(searchoptions);
			try {
				r=(int) db.findDouble(sql, params);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if(seacherType.equals("uid")){
			sql="select count(*) from haipumi_user where uid=? ";
			params.add(searchoptions);
			try {
				r=(int) db.findDouble(sql, params);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if(seacherType.equals("qq")){
			sql="select count(*) from haipumi_user where uqq=? ";
			params.add(searchoptions);
			try {
				r=(int) db.findDouble(sql, params);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return r;
	}

	@Override
	public haipumi_user insertCust(haipumi_user user) {
		List<Object>params=new ArrayList<Object>();
		String sql=null;
		sql=" insert into haipumi_user (ulogon,upwd,ualais,uimage,usex,uname,sid,uqq,ucname,ucpro,uclass,utel,uinfo,udatetime,ustatus,uwealth,urank,uglory,uvip) "+
				"values(?,?,?,default,default,default,default,default,default,default,default,default,default,default,default,default,default,default,default)";
		String md5pwd=Encrypt.md5(user.getUpwd());
		haipumi_user backuser=null;
		params.add(user.getUlogon());
		params.add(md5pwd);
		params.add(user.getUalais());
		int result=0;
		try {
			result=db.insertWithKey(sql, params);
			if(result>0){
				//成功
				backuser=findUser(result);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return backuser;
	}

	@Override
	public haipumi_user findUser(int uid) {
		String sql=null;
		sql=" select * from haipumi_user where uid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(uid);
		haipumi_user backuser=null;
		try {
			List<haipumi_user>backuserlist=db.finds(sql, params, haipumi_user.class);
			backuser=backuserlist.get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return backuser;
	}

	@Override
	public boolean updateUserImage(haipumi_user user) {
		String sql=null;
		sql=" update haipumi_user set uimage=? where uid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(user.getUimage());
		params.add(user.getUid());
		int result=0;
		boolean flag=false;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				//修改成功了
				flag=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public haipumi_user updateUser(haipumi_user user) {
		boolean flag=false;
		String sql;
		sql=" update haipumi_user set ulogon=?,upwd=?,ualais=?,uwealth=?,urank=?,uvip=?,uglory=? where uid=? ";
		List<Object>params=new ArrayList<Object>();
		String oldpwd=findUser(Integer.parseInt(user.getUid())).getUpwd();
		String newpwd=user.getUpwd();
		String md5pwd=Encrypt.md5(user.getUpwd());
		if(oldpwd.equals(newpwd)){
			//密码相同，不加密了
			params.add(user.getUlogon());
			params.add(user.getUpwd());
			params.add(user.getUalais());
			params.add(user.getUwealth());
			params.add(user.getUrank());
			params.add(user.getUvip());
			params.add(user.getUglory());
			params.add(user.getUid());
		}else{
			//密码不相同，加密了
			params.add(user.getUlogon());
			params.add(md5pwd);
			params.add(user.getUalais());
			params.add(user.getUwealth());
			params.add(user.getUrank());
			params.add(user.getUvip());
			params.add(user.getUglory());
			params.add(user.getUid());
		}
		int result=0;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				//成功了，将现在的user返回
				if(!oldpwd.equals(newpwd)){
					user.setUpwd(md5pwd);
				}
			}else{
				user=findUser(Integer.parseInt(user.getUid()));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public boolean updatesimage(haipumi_skin skin) {
		String sql=null;
		sql=" update haipumi_skin set simage=? where sid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(skin.getSimage());
		params.add(skin.getSid());
		int result=0;
		boolean flag=false;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				//修改成功了
				flag=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public haipumi_skin findskin(int sid) {
		String sql=null;
		sql=" select * from haipumi_skin where sid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(sid);
		haipumi_skin newskin=null;
		try {
			List<haipumi_skin>newskinlist=db.finds(sql, params, haipumi_skin.class);
			newskin=newskinlist.get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newskin;
	}

	@Override
	public boolean acphoto(haipumi_activity activity) {
		String sql=null;
		sql=" update haipumi_activity set acphoto=? where acid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(activity.getAcphoto());
		params.add(activity.getAcid());
		int result=0;
		boolean flag=false;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				//修改成功了
				flag=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public haipumi_activity findactivity(int acid) {
		String sql=null;
		sql=" select * from haipumi_activity where acid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(acid);
		haipumi_activity newactivity=null;
		try {
			List<haipumi_activity>newactivitylist=db.finds(sql, params, haipumi_activity.class);
			newactivity=newactivitylist.get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newactivity;
	}

	

}
